package com.koron.css2.newProjectCenter.callback;

import com.koron.css2.clientCenter.Enum.ProcessStateEnum;
import com.koron.css2.newProjectCenter.bean.PjCollection;
import com.koron.css2.newProjectCenter.bean.query.PjCollectionQuery;
import com.koron.css2.newProjectCenter.mapper.PjCollectionMapper;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.workflow.ProcessInterface;
import com.koron.util.Constant;
import com.koron.util.DateUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.Date;

/**
 * 工程报装-工程收款-工作流回调接口
 *
 * @author 朱超
 * @since 2022-01-19 10:44:39
 */
public class PjCollectionCallback implements ProcessInterface {

    @Override
    public MessageBean<?> before(SessionFactory factory, UserInfoBean userInfo, String billId, String fromNodeCode, int operation) {
        try {
            PjCollectionMapper pjCollectionMapper = factory.getMapper(PjCollectionMapper.class);

            if (operation == OPERATION_SUSPEND){
                PjCollectionQuery pjCollectionQuery = new PjCollectionQuery();
                pjCollectionQuery.setId(billId);
                PjCollection pjCollection = pjCollectionMapper.getSingle(pjCollectionQuery);

                pjCollection.setStatus(3);

                pjCollectionMapper.update(pjCollection);
            }

            // 1. 如果是审批通过
            if (operation == OPERATION_AGREE && fromNodeCode.equals(ProcessStateEnum.PROCESSING.getState())) {
                // 1.1 根据id查询单条数据
                PjCollectionQuery pjCollectionQuery = new PjCollectionQuery();
                pjCollectionQuery.setId(billId);
                PjCollection pjCollection = pjCollectionMapper.getSingle(pjCollectionQuery);
                // 1.2 updateTime,updateAccount,updateName
                pjCollection.setUpdateInfo(userInfo);
                // 1.3 去掉money和notes
                pjCollection.setMoney(null);
                pjCollection.setNotes(null);
                // 1.4 status
                pjCollection.setStatus(1);
                // 1.5 更新数据
                pjCollectionMapper.update(pjCollection);

            }
            // 2. 如果是待收款通过
            if (operation == OPERATION_AGREE && fromNodeCode.equals(ProcessStateEnum.COLLECTION.getState())) {
                // 2.1 根据id查询单条数据
                PjCollectionQuery pjCollectionQuery = new PjCollectionQuery();
                pjCollectionQuery.setId(billId);
                PjCollection pjCollection = pjCollectionMapper.getSingle(pjCollectionQuery);
                // 2.2 paymentDate
                pjCollection.setPaymentDate(DateUtils.parseDate(new Date(),"yyyy-MM-dd HH:mm:ss"));
                // 2.3 updateTime,updateAccount,updateName
                pjCollection.setUpdateInfo(userInfo);
                // 2.4 去掉money和notes
                pjCollection.setMoney(null);
                pjCollection.setNotes(null);
                // 2.5 status
                pjCollection.setStatus(2);
                // 2.6 更新数据
                pjCollectionMapper.update(pjCollection);
            }

            return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "工程收款提交成功", null);
        } catch (Exception e) {
            logger.error("工程收款提交失败", e);
        }
        return MessageBean.create(Constant.MESSAGE_INT_FAIL, "工程收款提交失败", null);
    }

    @Override
    public MessageBean<?> after(SessionFactory factory, UserInfoBean userInfo, String billId, String fromNodeCode, String toNodeCode) {
        return null;
    }
}
